import os
import time
from os import system
from random import randint
from ttkbootstrap import Style, Progressbar
from tkinter import *
from tkinter.filedialog import (askopenfilename)
from tkinter.messagebox import showinfo, showwarning

style = Style(theme='superhero')
window = style.master
filename = ''
text_read = ''
as_tool = '需要设置'

window.geometry("500x300")
window.maxsize(500, 300)
window.title("Au!SuxydsPC")
'''
定义函数
'''


def exit():
    sys.exit(0)


def use_script():
    '''
    用于切换脚本
    '''
    global as_tool
    as_tool = askopenfilename()
    log_show.insert(END, "已设置脚本为:" + as_tool)


def out_exe():
    global filename
    log_show.insert(END, '开始生成，当前启用pyinstall.exe')
    log_show.insert(END, "请勿其他操作以免程序崩溃！")
    time.sleep(1)
    info = os.popen("pyinstaller.exe -F " + filename + " -w --key suxyds").read()
    log_show.insert(END, info)
    log_show.insert(END, '[!]输出路径:' + os.getcwd() + "/dist/" + filename.split("/")[-1].strip(".py") + ".exe")


def begin_as():
    global text_read, filename, as_tool
    if as_tool == '需要设置':
        showwarning('提醒', '请先导入脚本')
    else:
        print(filename)
        log_show.insert(END, '开始混淆' + filename, '\n混淆脚本:' + as_tool)
        print(filename)
        run_boot = as_tool
        if as_tool == 'MB64For3.exe':
            run_boot = r".\Script\MB64For3\MB64For3.exe"
        os.system(str(run_boot + " " + '"' + str(filename) + '"'))
        code_show.delete(0, END)
        with open(filename, 'r') as read_app:
            read_result = read_app.read().split("\n")
        for i in read_result:
            code_show.insert(END, i + "\n\n")
        for i in read_result:
            text_read = text_read + i + '\n'


def show_asTool():
    log_show.insert(END, '当前工具:', as_tool)
    showinfo("当前混淆", as_tool)


def new_code():
    showwarning("抱歉", '功能暂未开发完成，请稍后')


def infomation():
    showinfo(
        '关于程序',
        '''版本:1.0
        开发团队:Suxyds
        开发成员:liusuxyds
            ''')


def introduction_code():
    '''
    文件选择函数
    自动读取
    '''
    global text_read, filename
    filename2 = askopenfilename()
    log_show.insert(END, 'Filename:' + filename2)
    code_show.delete(0, END)
    filename = filename2
    with open(filename2, 'r') as read_app:
        read_result = read_app.read().split("\n")
    for i in read_result:
        code_show.insert(END, i + "\n\n")
    for i in read_result:
        text_read = text_read + i + '\n'


'''
定义控件
'''
fd = Progressbar(bootstyle="success")
fd.place(height=43, width=95, x=1, y=142)
for i in range(100):
    fd['value'] = i + 1
    window.update()
    time.sleep(0.001)

ttk.Button(window, command=introduction_code, text="导入代码", style='primary.Outline.TButton', width=10).place(x=1, y=1)
ttk.Button(window, command=show_asTool, text="查看混淆器", style='success.Outline.TButton', width=10).place(x=1, y=35)
ttk.Button(window, command=exit, text="退出核心", style='info.Outline.TButton', width=10).place(x=1, y=70)
ttk.Button(window, command=infomation, text="关于程序", style='warning.Outline.TButton', width=10).place(x=1, y=105)

ttk.Button(window, command=begin_as, text="开始混淆", style='success-link', width=10).place(x=400, y=190)
ttk.Button(window, command=out_exe, text="导出exe", style='success-link', width=10).place(x=400, y=225)
ttk.Button(window, command=use_script, text="导入脚本", style='success-link', width=10).place(x=400, y=260)

code_show = Listbox(window, width=56)
code_show.place(x=100, y=1)
log_show = Listbox(window, width=56, height=6)
log_show.place(x=0, y=188)

window.mainloop()
